from urllib.request import Request

from fastapi import FastAPI, Depends
from app.core.logger import log_requests, setup_logging

app = FastAPI()

logger = setup_logging()  # 初始化日志

# 挂载请求日志中间件
@app.middleware("http")
async def auto_log(request: Request, call_next):
    return await log_requests(request, call_next)

# 业务路由示例
@app.get("/")
async def home():
    logger.info("Homepage accessed")
    logger.warn("warning info")
    logger.debug("debug info")
    logger.error("error info")
    logger.critical("critical info")
    logger.exception("exception info")
    return {"message": "Hello World"}

@app.get("/users/{user_id}")
async def get_user(user_id: int):
    logger.debug(f"Fetching user {user_id}")  # 调试信息
    return {"user_id": user_id}

if __name__ == "__main__":
    import uvicorn

    uvicorn.run("main:app", host='127.0.0.1', port=8000, log_level="info", reload=True)
